package com.surenpi.jenkins.client.job;

import com.surenpi.jenkins.client.BaseManager;
import com.surenpi.jenkins.client.core.JenkinsInfo;
import com.surenpi.jenkins.client.folder.FolderJob;
import com.surenpi.jenkins.client.folder.Folders;
import com.surenpi.jenkins.client.util.EncodingUtils;
import com.surenpi.jenkins.client.util.UrlUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Jobs extends BaseManager {
   public void create(FolderJob folder, String jobName, String jobXml, Boolean crumFlag) throws IOException {
      String path = UrlUtils.toBaseUrl(folder) + "createItem?name=" + EncodingUtils.encodeParam(jobName);
      this.getClient().postXml(path, jobXml, crumFlag);
   }

   public void create(FolderJob folderJob, String jobName, String jobXml, boolean crumFlag, boolean createFolder) throws IOException {
      if (createFolder) {
         Folders folders = new Folders();
         folders.setClient(this.getClient());
         boolean exists = folders.exists(folderJob.getName());
         if (!exists) {
            folders.create(folderJob.getName());
         }
      }

      this.create(folderJob, jobName, jobXml, crumFlag);
   }

   public void create(FolderJob folderJob, String jobName, String jobXml) throws IOException {
      String path = UrlUtils.toBaseUrl(folderJob) + "createItem?name=" + EncodingUtils.encodeParam(jobName);
      this.getClient().postXml(path, jobXml);
   }

   public void create(String jobName, String jobXml, Boolean crumFlag) throws IOException {
      this.create((FolderJob)null, jobName, jobXml, crumFlag);
   }

   public void create(String jobName, String jobXml) throws IOException {
      this.create(jobName, jobXml, this.isCrumb());
   }

   public void copy(String originName, String newName) throws IOException {
      this.getClient().post(String.format("/createItem?mode=copy&from=%s&name=%s", EncodingUtils.encodeParam(originName), EncodingUtils.encodeParam(newName)));
   }

   public void update(FolderJob folderJob, String jobName, String jobXml, Boolean crumbFlag) throws IOException {
      String path = UrlUtils.toJobBaseUrl(folderJob, jobName) + "/getXml.xml";
      this.getClient().postXml(path, jobXml, crumbFlag);
   }

   public void update(String jobName, String jobXml, Boolean crumbFlag) throws IOException {
      this.update((FolderJob)null, jobName, jobXml, crumbFlag);
   }

   public void update(String jobName, String jobXml) throws IOException {
      this.update(jobName, jobXml, this.isCrumb());
   }

   public void delete(FolderJob folderJob, String jobName, Boolean crumbFlag) throws IOException {
      String path = UrlUtils.toJobBaseUrl(folderJob, jobName) + "/doDelete";
      this.getClient().post(path, crumbFlag);
   }

   public void delete(String jobName, Boolean crumbFlag) throws IOException {
      this.delete((FolderJob)null, jobName, crumbFlag);
   }

   public void delete(String jobName) throws IOException {
      this.delete(jobName, this.isCrumb());
   }

   public int batchDel(String prefixName) throws IOException {
      List<Job> allJobs = this.getAllJobs();
      int count = 0;
      if (allJobs == null) {
         return count;
      } else {
         for(Job job : allJobs) {
            if (job.getName().startsWith(prefixName)) {
               this.delete(job.getName());
               ++count;
            }
         }

         return count;
      }
   }

   public void rename(FolderJob folderJob, String oldName, String newName, Boolean crumbFlag) throws IOException {
      String path = UrlUtils.toJobBaseUrl(folderJob, oldName) + "/doRename?newName=" + EncodingUtils.encodeParam(newName);
      this.getClient().post(path, crumbFlag);
   }

   public void rename(String oldName, String newName, Boolean crumbFlag) throws IOException {
      this.rename((FolderJob)null, oldName, newName, crumbFlag);
   }

   public void rename(String oldName, String newName) throws IOException {
      this.rename(oldName, newName, this.isCrumb());
   }

   public String getXml(FolderJob folderJob, String jobName) throws IOException {
      return this.getClient().get(UrlUtils.toJobBaseUrl(folderJob, jobName) + "/config.xml");
   }

   public String getXml(String jobName) throws IOException {
      return this.getXml((FolderJob)null, jobName);
   }

   public void disable(String jobName, Boolean crumbFlag) throws IOException {
      this.getClient().post("/job/" + EncodingUtils.encode(jobName) + "/disable", this.isCrumb());
   }

   public void disable(String jobName) throws IOException {
      this.disable(jobName, this.isCrumb());
   }

   public void enable(String jobName, Boolean crumbFlag) throws IOException {
      this.getClient().post("/job/" + EncodingUtils.encode(jobName) + "/enable", this.isCrumb());
   }

   public void enable(String jobName) throws IOException {
      this.enable(jobName, this.isCrumb());
   }

   public void build(String jobName) throws IOException {
      this.getClient().post("/job/" + EncodingUtils.encode(jobName) + "/build", this.isCrumb());
   }

   public void build(String folder, String jobName, String branch) throws IOException {
      this.getClient().post("/job/" + EncodingUtils.encode(folder) + "/job/" + EncodingUtils.encode(jobName) + "/job/" + EncodingUtils.encode(branch) + "/build", this.isCrumb());
   }

   public void buildWithParams(String jobName, Map<String, String> params) throws IOException {
      StringBuffer urlBuf = new StringBuffer();
      urlBuf.append("/job/");
      urlBuf.append(EncodingUtils.encode(jobName));
      urlBuf.append("/buildWithParameters/?1=1");

      for(String key : params.keySet()) {
         String value = (String)params.get(key);
         urlBuf.append("&").append(key).append("=").append(value);
      }

      this.getClient().post(urlBuf.toString(), this.isCrumb());
   }

   public String getLogText(String jobName, int buildNum) throws IOException {
      return this.getClient().get("/job/" + EncodingUtils.encode(jobName) + "/" + buildNum + "/consoleText");
   }

   public void stop(String jobName, int buildId) throws IOException {
      this.getClient().post("/job/" + EncodingUtils.encode(jobName) + "/" + buildId + "/stop", this.isCrumb());
   }

   public JenkinsInfo getAll() throws IOException {
      return (JenkinsInfo)this.getClient().get("/", JenkinsInfo.class);
   }

   public List<Job> getAllJobs() throws IOException {
      JenkinsInfo jenkinsInfo = this.getAll();
      return jenkinsInfo.getJobs();
   }

   public JobDetails getDetails(String jobName) throws IOException {
      return (JobDetails)this.getClient().get("/job/" + EncodingUtils.encode(jobName), JobDetails.class);
   }

   public GitTagList getGitTagList(String jobName, String parameterName) throws IOException {
      return (GitTagList)this.getClient().get("/job/" + EncodingUtils.encode(jobName) + "/descriptorByName/net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition/fillValueItems?param=" + EncodingUtils.encode(parameterName), GitTagList.class);
   }

   public BuildDetail getBuildDetails(String jobName, int number) throws IOException {
      return (BuildDetail)this.getClient().get("/job/" + EncodingUtils.encode(jobName) + "/" + number, BuildDetail.class);
   }

   public BuildDetail getLastBuildDetails(String jobName) throws IOException {
      JobDetails jobDetails = this.getDetails(jobName);
      int lastBuildNum = jobDetails.getLastBuild().getNumber();
      return this.getBuildDetails(jobName, lastBuildNum);
   }

   protected String[] getDependencyArray() {
      return new String[0];
   }
}
